﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SatisAktarim
{
    public partial class frmAktarim : Form
    {
        bool Calisiyor = true;
        Aktarim aktar = new Aktarim();
        bool DemoKullanim = false;
        public frmAktarim(bool demo)
        {
            InitializeComponent();
            timer1.Interval = Program.Session[Settings.GuncellemeSuresi].ToString().ToInt() * 1000;
            DemoKullanim = demo;
        }

        private void frmAktarim_Load(object sender, EventArgs e)
        {
            dteTarih.Value = DateTime.Now;
            timer1.Start();
        }

        private void mnManage_Click(object sender, EventArgs e)
        {
            this.Show();
        }

        private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            object[] arg = (object[])e.Argument;
            DateTime tarih;
            switch (Convert.ToByte(arg[0]))
            {
                case 0:
                    tarih = Convert.ToDateTime(arg[1]);
                    aktar.Start(tarih);
                    break;
                case 1:
                    tarih = Convert.ToDateTime(arg[1]);
                    aktar.YenidenAktar(tarih);
                    aktar.Start(tarih);
                    break;
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (Calisiyor)
            {
                Calisiyor = false;
                btnStart.Text = "Başlat";
                timer1.Stop();
                timer1.Enabled = false;
                lblStatus.Text = "Durum : Duraklatıldı...";
            }
            else
            {
                Calisiyor = true;
                btnStart.Text = "Durdur";
                timer1.Enabled = true;
                timer1.Start();
                lblStatus.Text = "Durum : Çalışıyor...";
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!bgWorker.IsBusy)
            {
                timer1.Stop();
                timer1.Enabled = false;
                bgWorker.WorkerSupportsCancellation = true;
                object[] arg = new object[2];
                arg[0] = 0;
                arg[1] = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
                bgWorker.RunWorkerAsync(arg);
            }
        }

        private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (!DemoKullanim)
            {
                timer1.Enabled = true;
                timer1.Start();
            }
            else
            {
                MessageBox.Show("Demo kullanım bitti !");
                Application.Exit();
            }
        }

        private void btnCalistir_Click(object sender, EventArgs e)
        {
            if (rdYenidenAktar.Checked)
            {
                if (!bgWorker.IsBusy)
                {
                    timer1.Stop();
                    timer1.Enabled = false;
                    bgWorker.WorkerSupportsCancellation = true;
                    object[] arg = new object[2];
                    arg[0] = 1;
                    arg[1] = new DateTime(dteTarih.Value.Year, dteTarih.Value.Month, dteTarih.Value.Day);
                    bgWorker.RunWorkerAsync(arg);
                }
                else
                {
                    MessageBox.Show("Aktarım yapılıyor lütfen tekrar deneyiniz");
                }
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
        }

        private void frmAktarim_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
            }
        }

        bool ilk = true;
        private void frmAktarim_Activated(object sender, EventArgs e)
        {
            if (ilk)
            {
                ilk = false;
                this.Hide();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            frmAyarlar ayar = new frmAyarlar();
            ayar.ShowDialog(this);
        }
    }
}
